<%@ page pageEncoding="utf-8"%>
<%@ page import="org.jawa.admin.util.ParamUtils"%>
<%@ page import="org.jawa.core.util.JawaGlobals"%>
<%@ page import="org.jawa.core.util.StringUtils"%>
<%@ page import="org.jawa.core.JawaServer"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Locale"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.StringTokenizer"%>
<%
	// 系统已安装
	if (!JawaServer.getInstance().isSetupMode()) {
        response.sendRedirect("setup-completed.jsp");
        return;
    }
%>
<%!boolean testConnection(Map<String, String> errors, String defaultFrom, String host, String username, String password) {
		boolean success = true;

		return success;
	}

	Map<String, String> validatorMail(String defaultFrom, String host, String username, String password) {
		Map<String, String> errors = new HashMap<String, String>();
		if (defaultFrom == null || defaultFrom.trim().equals("")) {
			errors.put("from", "默认发件人");
		}
		if (host == null || host.trim().equals("")) {
			errors.put("host", "");
		}

		return errors;
	}%>
<%
	// Get parameters

	String defaultFrom = ParamUtils.getParameter(request, "defaultFrom");
	String host = ParamUtils.getParameter(request, "host");
	int port = ParamUtils.getIntParameter(request, "port", 0);
	String username = ParamUtils.getParameter(request, "username", true);
	String password = ParamUtils.getParameter(request, "password", true);
	boolean debug = ParamUtils.getBooleanParameter(request, "debug");
	boolean ssl = ParamUtils.getBooleanParameter(request, "ssl");

	boolean doContinue = request.getParameter("continue") != null;

	Map<String, String> errors = new HashMap<String, String>();
	if (doContinue) {
		errors = validatorMail(defaultFrom, host, username, password);
		if (errors.size() == 0) {
			JawaGlobals.setProperty("mail.default.from", defaultFrom);
			JawaGlobals.setProperty("mail.smtp.host", host);
			JawaGlobals.setProperty("mail.smtp.port",  Integer.toString(port));
			JawaGlobals.setProperty("mail.smtp.username", username);
			JawaGlobals.setProperty("mail.smtp.password", password);
			JawaGlobals.setProperty("mail.smtp.ssl", Boolean.toString(ssl));
			JawaGlobals.setProperty("mail.debug", Boolean.toString(debug));
			if (testConnection(errors, defaultFrom, host, username, password)) {
				// 成功
				response.sendRedirect("setup-admin-settings.jsp");
				return;
			}
		}
	}

	if (!doContinue) {
		try {
			defaultFrom = JawaGlobals.getProperty("mail.default.from");
			host = JawaGlobals.getProperty("mail.smtp.host");
			port = JawaGlobals.getIntProperty("mail.smtp.port",25);
			username = JawaGlobals.getProperty("mail.smtp.username");
			password = JawaGlobals.getProperty("mail.smtp.password");
			ssl = JawaGlobals.getBooleanProperty("mail.smtp.ssl");
			debug = JawaGlobals.getBooleanProperty("mail.debug");
		} catch (Exception e) {
		}
	}
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>邮件设置</title>
		<meta name="currentStep" content="4" />
	</head>
	<body>
		<div class="row-fluid">
			<div class="page-header">
				<h3>
					邮件设置
				</h3>
			</div>

			<%
				if (!errors.isEmpty() && doContinue) {
			%>
			<div class="alert alert-success fade in">

				<%
					if (errors.get("from") != null) {
				%>
				<strong>默认发件人设置错误!</strong>
				<%
					} else if (errors.get("host") != null) {
				%>
				<strong>邮件服务器设置错误!</strong>
				<%
					}
				%>

			</div>

			<%
				}
			%>
			<div class="well">
				<form action="setup-mail-settings.jsp" name="sform" method="post"
					class="form-horizontal">
					<div class="control-group  <%=errors.containsKey("from") ? "error" : ""%>">
						<label for="focusedInput" class="control-label">
							默认发件人:
						</label>
						<div class="controls">
							<input type="text" name="defaultFrom" value="<%=(defaultFrom != null) ? defaultFrom : ""%>"
								class="input-xlarge " />
						</div>
					</div>
					<div
						class="control-group <%=errors.containsKey("host") ? "error" : ""%>">
						<label for="focusedInput" class="control-label">
							邮件服务器:
						</label>
						<div class="controls">
							<input type="text" name="host"
								value="<%=(host != null) ? host : ""%>" class="input-xlarge "
								maxlength="150" />
							<%
								if (errors.containsKey("host")) {
							%>
							<span class="help-inline"> <%=errors.get("host")%> </span>
							<%
								}
							%>
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器端口:
						</label>
						<div class="controls">
							<input type="text" name="port"
								value="<%=(port > 0) ? String.valueOf(port) : ""%>"
								class="input-xlarge " maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器用户名:
						</label>
						<div class="controls">
							<input type="text" name="server_username"
								value="<%=(username != null) ? username : ""%>"
								class="input-xlarge " maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							服务器密码:
						</label>
						<div class="controls">
							<input type="password" name="server_password"
								value="<%=(password != null) ? StringUtils.hash(password) : ""%>"
								class="input-xlarge focused" maxlength="15" />
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							使用SSL:
						</label>
						<div class="controls">
							<label class="checkbox inline">
								<input type="checkbox" name="ssl" <%=(ssl) ? " checked" : ""%>
									class="input-xlarge " />
							</label>
						</div>
					</div>
					<div class="control-group ">
						<label for="focusedInput" class="control-label">
							邮件调试:
						</label>
						<div class="controls">
							<label class="radio">
								<input type="radio" name="debug" value="true"
									<%=(debug ? " checked=\"checked\" " : "")%> id="rb01">
								打开
							</label>
							<label class="radio">
								<input type="radio" name="debug" value="false"
									<%=(!debug ? " checked=\"checked\" " : "")%> id="rb02">
								关闭
							</label>
						</div>
					</div>

					<div class="form-actions">
						<input type="button"  value="上一步" id="jawa-setup-prev" class="btn btn-info "  onclick="window.location.href='setup-clustering-settings.jsp'" />
						<input type="submit" name="continue" value="下一步" id="jawa-setup-next" class="btn btn-primary " />
					</div>
					
				</form>

			</div>
		</div>
	</body>
</html>
